package lily.ui.bookshelf;

import java.util.List;
import java.util.Map;

import lily.phs.urh.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShelfAdapter extends BaseAdapter{

	private static final int COLUMNS = 5;
	private Context mContext;
	private List<Map<String, Object>> fileList;//档案列表
	private int sum;//档案的实际数
	private int total;//总item数
	private LayoutInflater inflater;
	
	public ShelfAdapter(Context context, List<Map<String, Object>> list) {
		super();
		this.mContext = context;
		this.fileList = list;
		this.sum = list.size();
		total = Math.max(((sum-1)/COLUMNS+1)*COLUMNS,30);
		
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return total;
	}
	
	public void setSum(int num){
		this.sum = num;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub                                                                                                                                    
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(convertView==null){
			convertView = inflater.inflate(R.layout.shelf_item, null);
			holder = new ViewHolder();
			holder.llFile = (LinearLayout)convertView.findViewById(R.id.file);
			holder.tvName = (TextView)convertView.findViewById(R.id.file_name);
			holder.tvId = (TextView)convertView.findViewById(R.id.file_id);
			convertView.setTag(holder);
		}
		else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		if(position<sum){
			String here_id = fileList.get(position).get("item_here_id").toString();
			int id = Integer.parseInt(fileList.get(position).get("item_id").toString());
			String name = fileList.get(position).get("item_name").toString();
			holder.tvName.setText(name);
			holder.tvId.setText(String.valueOf(here_id));
			convertView.setId(id);
		}
		else{
			holder.llFile.setVisibility(View.GONE);
			convertView.setId(-1);
		}
		return convertView;
	}
	
	private class ViewHolder{
		LinearLayout llFile;
		TextView tvName;
		TextView tvId;
	}

}
